package com.qf.osmproduct.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qf.common.core.entity.product.Product;
import com.qf.common.core.entity.product.UserFavorite;
import com.qf.common.core.util.UserUtils;
import com.qf.osmproduct.product.mapper.ProductMapper;
import com.qf.osmproduct.product.mapper.UserFavoriteMapper;
import com.qf.osmproduct.product.param.UserFavoriteParam;
import com.qf.osmproduct.product.service.UserFavoriteService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author GaoQiangLiang
 * @date 2025-06-13 9:22
 */

@Service
public class UserFavoriteServiceImpl implements UserFavoriteService {


    @Resource
    private UserFavoriteMapper userFavoriteMapper;

    @Resource
    private ProductMapper productMapper;


    /**
     * 添加收藏
     *
     * @param userFavoriteParam 收藏参数
     * @return true-成功 false-失败
     */
    @Override
    public boolean addFavorite(UserFavoriteParam userFavoriteParam) {
        // 获取当前用户id
        Integer uid = UserUtils.getUid();


        // 1. 首先判断是否存在
        UserFavorite isFavorite = userFavoriteMapper.selectOne(Wrappers.lambdaQuery(UserFavorite.class)
                .eq(UserFavorite::getUserId, uid)
                .eq(UserFavorite::getIsCollected, 1)
                .eq(UserFavorite::getObjectId, userFavoriteParam.getObjectId()));

        if (isFavorite != null) {
            return false;
        }


        // 1. 首先判断用户在收藏表中是否有记录存在
        UserFavorite userFavoriteResult = userFavoriteMapper.selectOne(Wrappers.lambdaQuery(UserFavorite.class)
                .eq(UserFavorite::getUserId, uid)
                .eq(UserFavorite::getObjectId, userFavoriteParam.getObjectId()));

        // 2. 如果有记录存在，则需要将is_collected字段设置为1，表示收藏
        if (userFavoriteResult != null) {
            // 更新收藏状态
            userFavoriteMapper.update(null, Wrappers.lambdaUpdate(UserFavorite.class)
                    .eq(UserFavorite::getUserId, uid)
                    .eq(UserFavorite::getObjectId, userFavoriteParam.getObjectId())
                    .eq(UserFavorite::getObjectType, userFavoriteParam.getObjectType())
                    .set(UserFavorite::getIsCollected, 1)
                    .set(UserFavorite::getUpdateTime, new java.util.Date())
                    .set(UserFavorite::getCreateTime, new java.util.Date()));

        } else {
            // 3. 如果没有记录存在，则需要插入一条新的记录到收藏表中
            UserFavorite userFavorite = new UserFavorite();
            userFavorite.setUserId(Long.valueOf(uid));
            userFavorite.setObjectId(Long.valueOf(userFavoriteParam.getObjectId()));
            userFavorite.setObjectType(userFavoriteParam.getObjectType());
            userFavorite.setIsCollected(1);
            userFavorite.setCreateTime(new java.util.Date());
            userFavorite.setUpdateTime(new java.util.Date());

            // 4. 然后向收藏表插入数据
            userFavoriteMapper.insert(userFavorite);
        }
        // 6. 更新商品表中的收藏数量
        int update_goods_fav = productMapper.update(null, Wrappers.lambdaUpdate(Product.class)
                .eq(Product::getProductId, userFavoriteParam.getObjectId())
                .setSql("product_goods_fav = product_goods_fav + 1"));
        return update_goods_fav > 0;
    }

    /**
     * 删除收藏
     *
     * @param favoriteParam 收藏参数
     * @return true-成功 false-失败
     */
    @Override
    public boolean removeFavorite(UserFavoriteParam favoriteParam) {
        // 获取当前用户id
        Integer uid = UserUtils.getUid();

        // 1. 首先判断用户在收藏表中是否有记录存在
        UserFavorite userFavoriteResult = userFavoriteMapper.selectOne(Wrappers.lambdaQuery(UserFavorite.class)
                .eq(UserFavorite::getUserId, uid)
                .eq(UserFavorite::getObjectId, favoriteParam.getObjectId()));

        // 2. 如果有记录存在，继续判断，如果is_collected字段为1，则需要将is_collected字段设置为0，表示取消收藏
        if (userFavoriteResult != null) {
            // 更新收藏状态
            int updated = userFavoriteMapper.update(null, Wrappers.lambdaUpdate(UserFavorite.class)
                    .eq(UserFavorite::getUserId, uid)
                    .eq(UserFavorite::getObjectId, favoriteParam.getObjectId())
                    .eq(UserFavorite::getObjectType, favoriteParam.getObjectType())
                    .set(UserFavorite::getIsCollected, 0)
                    .set(UserFavorite::getUpdateTime, new java.util.Date())
                    .set(UserFavorite::getCreateTime, new java.util.Date()));
            if (updated > 0) {
                // 更新商品表中的收藏数量
                int updateGoodsFav = productMapper.update(null, Wrappers.lambdaUpdate(Product.class)
                        .eq(Product::getProductId, favoriteParam.getObjectId())
                        .setSql("product_goods_fav = product_goods_fav - 1")
                        .set(Product::getProductCreateDate, new java.util.Date()));
                return updateGoodsFav > 0;

            } else {
                // 更新失败
                throw new RuntimeException("更新收藏状态失败");
            }
        }

        // 3. 如果没有收藏过，则直接返回false
        return false;

    }
}
